package com.xuzili.wechat.servise.impl;

import com.xuzili.wechat.entity.Weather;
import com.xuzili.wechat.servise.TainXingService;
import com.xuzili.wechat.servise.WeChatService;
import com.xuzili.wechat.servise.WeatherService;
import com.xuzili.wechat.utils.DateCalculationUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author xu
 * @date:2022/8/2118:54
 */
@Service
public class WeChatServiceImpl implements WeChatService {
    @Value("${weChat.appID}")
    private String appId;

    @Value("${weChat.appsecret}")
    private String secret;

    @Value("${weChat.templateId}")
    private String TEMPLATE_ID;

    @Value("${weChat.userId}")
    private String USER_ID;

    @Value("${date.love}")
    private String anniversary;

    @Value("${date.boy}")
    private String BOY_BIRTHDAY;

    @Value("${date.girl}")
    private String GIRL_BIRTHDAY;
    @Value("${nickname}")
    private String nickname;

    @Resource
    private TainXingService tainXingService;
    @Resource
    private WeatherService weatherService;

    @Override
    public String pushMessage() {

        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(USER_ID)
                .templateId(TEMPLATE_ID)
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        Weather weather = weatherService.getWeather();
        Map<String, String> map = tainXingService.getEenglishSentence();
        templateMessage.addData(
                new WxMpTemplateData("riqi",weather.getDate() + "  "+ weather.getWeek()+"\r\n","#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",weather.getText_now()+"\r\n","#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low",weather.getLow() + "度\r\n","#EE212D"));
        templateMessage.addData(new WxMpTemplateData("temp",weather.getTemp() + "度\r\n","#EE212D"));
        templateMessage.addData(new WxMpTemplateData("high",weather.getHigh()+ "度\r\n","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("windclass",weather.getWind_class()+ "\r\n","#42B857" ));
        templateMessage.addData(new WxMpTemplateData("winddir",weather.getWind_dir()+ "\r\n","#B95EA3" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi",tainXingService.getCaiHongPi()+"\r\n","#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai",DateCalculationUtils.getAnniversaryDay(anniversary)+"\r\n","#FF1493"));
        templateMessage.addData(new WxMpTemplateData("shengri1",DateCalculationUtils.getBirthday(GIRL_BIRTHDAY)+"\r\n","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("birthDay",DateCalculationUtils.getBirthday(BOY_BIRTHDAY)+"天\r\n","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("en",map.get("en") +"\r\t","#C71585"));
        templateMessage.addData(new WxMpTemplateData("zh",map.get("zh") +"\r\t","#C71585"));

//        if(DateCalculationUtils.getAnniversaryDay(anniversary) % 365 == 0){
//            nickname = "今天是恋爱" + (DateCalculationUtils.getAnniversaryDay(anniversary) / 365) + "周年纪念日！";
//        }
//        if(DateCalculationUtils.getBirthday(GIRL_BIRTHDAY)  == 0){
//            nickname = "今天是臭啾啾生日，生日快乐呀！";
//        }
//        if(DateCalculationUtils.getBirthday(BOY_BIRTHDAY)  == 0){
//            nickname = "今天是臭灰灰生日，生日快乐呀！";
//        }
        templateMessage.addData(new WxMpTemplateData("beizhu",nickname,"#FF0000"));


        System.out.println(templateMessage.toJson());
        String result = null;
        try {
            result = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println(result);
        } catch (WxErrorException e) {
            System.out.println(e.getError().getErrorMsg());
            System.out.println(e.getError().getErrorCode());
            e.printStackTrace();
        }



        return "success";
    }
}
